package com.xilada.xldutils.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;

import com.xilada.xldutils.R;
import com.xilada.xldutils.utils.DensityUtil;
import com.xilada.xldutils.utils.Toast;
import com.xilada.xldutils.widget.ProgressDialog;

/**
 *
 * Created by LiaoXiang on 2015/11/18.
 */
public class BaseFragment extends Fragment{

    private ProgressDialog dialog;
    protected Context context;
    protected int mScreenWidth;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
        mScreenWidth = DensityUtil.getDeviceWidth(context);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }
    /**
     * 查找	View
     * @param resId 资源ID。
     * @return view
     */
    public final <T extends View>T findViewById(int resId) {
        if (getView()==null){
            return null;
        }
        //noinspection unchecked
        return (T)getView().findViewById(resId);
    }
    /**
     * 显示加载提示窗
     */
    protected void showDialog(){
        showDialog("加载中...");
    }

    /**
     * 显示加载提示窗
     * @param msg 提示文字
     */
    protected void showDialog(CharSequence msg){
        showDialog(msg,false);
    }

    /**
     * 显示加载提示窗
     * @param msg 提示文字
     * @param canCancel 是否可手动取消
     */
    protected void showDialog(CharSequence msg,boolean canCancel){
        if (context == null) {
            return;
        }
        if(dialog == null){
            dialog = new ProgressDialog(context, R.style.Theme_ProgressDialog);
        }
        dialog.setCanceledOnTouchOutside(canCancel);
        dialog.setMessage(msg);
        if (!dialog.isShowing()) {
            dialog.show();
        }
    }

    /**
     * 关闭加载窗
     */
    public void dismissDialog(){
        if (dialog != null && dialog.isShowing()){
            dialog.dismiss();
        }
    }

    /**
     * 显示Toast
     * @param msg 显示文字
     */
    public void showToast(String msg){
        if (context == null) {
            return;
        }
        Toast.create(context).show(msg);
    }
}
